<?php


namespace app\common\model;


use app\model\AdminAttendanceRecord;
use app\model\AdminAttendanceSys;
use app\model\AdminStaff;
use think\facade\Log;

class AttendanceRecordModel extends AdminAttendanceRecord
{
    // 后台考勤记录
    public static function getList(Array $data) {
        $size = $data['size'] ?? config('apiadmin.ADMIN_LIST_DEFAULT');
        $start = $data['page'] ?? 1;
        $sortKey = $data['sortKey'] ?? 'create_time';
        $order = $data['order'] ?? 'DESC';
        $student_no = $data['student_no'] ?? '';
        $student_name = $data['student_name'] ?? '';
        $phone = $data['phone'] ?? '';
        $school_id = $data['school_id'];
        $dormitory_id = $data['dormitory_id'] ?? 0;
        $student_dormitory_id = $data['student_dormitory_id'] ?? 0;
        $title = $data['title'] ?? '';
        $grade = $data['grade'] ?? '';

        $model = (new AdminAttendanceRecord())->alias('AdminAttendanceRecord');
        //规则名称
        if ($title) {
            $model->hasWhere('attendance', function ($query) use ($title) {
                $query->where('title', 'like', '%' . $title . '%');
            });
        }
        $staff_where = [];
        if ($student_no) {
            $staff_where[] = ['student_no', 'like', '%' . $student_no . '%'];
        }
        if ($student_name) {
            $staff_where[] = ['name', 'like', '%' . $student_name . '%'];
        }
        if ($phone) {
            $staff_where[] = ['phone', 'like', '%' . $phone . '%'];
        }
        if ($school_id > 0) {
            $staff_where[] = ['school_id', '=', $school_id];
        }
        //年级
        if ($grade) {
            $staff_where[] = ['grade', '=', $grade];
        }

        $icCard_where = [['is_delete', '=', '0']];
        if ($student_dormitory_id > 0) {
            $icCard_where[] = ['student_dormitory_id', '=', $student_dormitory_id];
        }
        if ($dormitory_id > 0) {
            $icCard_where[] = ['dormitory_id', '=', $dormitory_id];
        }
        if (!empty($data['punch_time']) && !is_array($data['punch_time'])) {
            $data['punch_time'] = explode( ",", $data['punch_time']);
        }
        if (!empty($data['punch_time']) && array_filter($data['punch_time'])) {
            // 自动选择到结束时间
            $data['punch_time'][1] = $data['punch_time'][1].' 23:59:59';
            $model->whereTime('AdminAttendanceRecord.create_time', 'between', $data['punch_time']);
        }
//        if (isset($data['scope'])) {
//            $model->where('clock_in_scope', $data['scope']);
//        }
        $model
            ->hasWhere('staff', $staff_where)
            ->hasWhere('icCard', $icCard_where)
            ->with(['attendance','icCard'=>['dormitory', 'student'],'staff'=>['school']])
            ->append(['status_text'])
            ->group('AdminAttendanceRecord.id')
            ->order('AdminAttendanceRecord.'.$sortKey, $order);
        if (!isset($data['export'])) {
            $list = $model->paginate(['page' => $start, 'list_rows' => $size])->toArray();
            return [
                'data' => $list['data'],
                'total' => $list['total']
            ];
        } else {
            $list = $model->select()->toArray();
            return [
                'data' => $list
            ];
        }
    }
}